<template>
  <div class="tinymce-editor">
    <textarea>你好，世界!</textarea>
  </div>
</template>
<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
/* Default icons are required. After that, import custom icons if applicable */
import 'tinymce/icons/default/icons.min.js';

/* Required TinyMCE components */
import 'tinymce/themes/silver/theme.min.js';
import 'tinymce/models/dom/model.min.js';

/* Import a skin (can be a custom skin instead of the default) */
import 'tinymce/skins/ui/oxide/skin.js';
import 'tinymce/skins/ui/oxide/content.min.css';

/* Import plugins */
import 'tinymce/plugins/advlist';
import 'tinymce/plugins/code';
import 'tinymce/plugins/emoticons';
import 'tinymce/plugins/emoticons/js/emojis';
import 'tinymce/plugins/link';
import 'tinymce/plugins/lists';
import 'tinymce/plugins/table';
import 'tinymce/plugins/image';

export default {
  components: {
    Editor
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    toolbar: {
      type: [String, Array],
      default: ' undo redo| styleselect | bold italic | alignleft aligncenter alignright alignjustify | ' +
          'bullist numlist outdent indent | image table| forecolor backcolor |AddGapBtn |'
    },
    height:{
      type:[Number],
      default: 150
    }
  },
  data() {
    let that = this;
    return {
      init: {
        language_url: `/tinymce/langs/zh-Hans.js`,
        language: 'zh_CN',
        skin_url: `/tinymce/skins/ui/oxide`,
        content_css: `/tinymce/skins/content/default/content.css`,
        // skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide-dark`, // 暗色系
        // content_css: `${this.baseUrl}/tinymce/skins/content/dark/content.css`, // 暗色系
        height: this.height,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        // paste_as_text:true,
        // 此处为图片上传处理函数，这个直接用了base64的图片形式上传图片，
        // 如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        images_upload_handler: async (blobInfo, success, failure) => {
          let filename = await this.$axios.post("/oss/get-filename", {fileName: blobInfo.filename()});
          let result = await this.$oss.put("code/" + filename, blobInfo.blob());
          success(this.VUE_APP_FILE_URL + "/code/" + filename)
        },
        setup: function(editor) {
          if(that.showGap){
            // 注册一个icon
            editor.ui.registry.addIcon(
                "add-gap",
                `<svg  width="24px" height="24px" t="1594110100878" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6830" width="200" height="200"><path d="M108.97 64.22h537.07c29.84 0 44.76 14.92 44.76 44.76v134.27c0 29.83-14.92 44.76-44.76 44.76H108.97c-29.84 0-44.76-14.92-44.76-44.76V108.98c0-29.84 14.92-44.76 44.76-44.76zM108.97 735.56h537.07c29.84 0 44.76 14.92 44.76 44.76v134.27c0 29.84-14.92 44.76-44.76 44.76H108.97c-29.84 0-44.76-14.92-44.76-44.76V780.32c0-29.84 14.92-44.76 44.76-44.76z" fill="#232F3E" p-id="6831"></path><path d="M377.5 399.89h537.07c29.84 0 44.76 14.92 44.76 44.76v134.27c0 29.84-14.92 44.76-44.76 44.76H377.5c-29.84 0-44.76-14.92-44.76-44.76V444.65c0.01-29.84 14.93-44.76 44.76-44.76zM254.11 449.75l-63.33 63.29 63.33 63.32-63.33 63.29L64.21 513.04l126.57-126.57z" fill="#3F9EFF" p-id="6832"></path></svg>`
            );
            // 注册一个自定义的按钮
            editor.ui.registry.addButton("AddGapBtn", {
              icon: `add-gap`,
              onAction: function(_) {
                let time = new Date().getTime()
                editor.insertContent(' <span  id='+editor.id+'_'+time+' style="color: #00a0e9 " class="gap_input">___</span> ')
                that.changeContent(Object.assign(editor,{editorInsertId:editor.id+'_'+time}))
              }
            });
          }

        },
      },
      myValue: this.value
    }
  },
  mounted() {
    tinymce.init({
      selector: 'textarea',
      language: 'zh_CN',
    })
  },
  methods: {
    // 添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    // 需要什么事件可以自己增加
    onClick(e) {
      // this.$emit('onClick', e, tinymce)
    },
    changeContent(e) {
      this.$emit('changeContent', e, tinymce)
    },
    // 可以添加一些自己的自定义事件，如清空内容
    clear() {
      this.myValue = ''
    }
  },
  watch: {
    value(newValue) {
      this.myValue = newValue
    },
    myValue(newValue) {
      this.$emit('input', newValue)
    }
  }
}
</script>

